package controllers;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;

import org.apache.log4j.Logger;

import keeperproperties.KeeperProperties;
import annotation.PropertyAnnotation;

public class Controller {
	private static KeeperProperties keeper_properties = KeeperProperties
			.getInstance();
	private static PropertyAnnotation annotation;
	private static HashMap<String, String> propertiesMap = new HashMap<String, String>();
	private static HashSet<String> temp = new HashSet<String>();
	private static final String ERROR="Exception by annotation";
	private static Logger log=Logger.getLogger(Controller.class);

	public static void setPropertyByAnnotation(Object o) {
		try {
			propertiesMap.putAll(keeper_properties.getProperties());
			temp.addAll(propertiesMap.keySet());
			Field[] fields = o.getClass().getFields();
			for (Field f : fields) {
				annotation = f.getAnnotation(PropertyAnnotation.class);
				String nameField = annotation.getConfigName().toString();
				for (String s : temp) {
					if (nameField.equals(s)) {
						String temp = propertiesMap.get(s);
						f.set(o, temp);
					}
				}

			}
		} catch (Exception e) {
			log.error(ERROR);
		}
	}

}
